//
//  DealerView.m
//  BORGWORD
//
//  Created by 张国庆 on 15/11/25.
//  Copyright © 2015年 zgq. All rights reserved.
//

#import "DealerView.h"

@implementation DealerView
@synthesize dealerArray=_dealerArray;
@synthesize dealerTabView=_dealerTabView;
@synthesize backGroundView=_backGroundView;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)initWithFrame:(CGRect)frame andArr:(NSMutableArray *)arr andSuperView:(UIView *)superView
{
    self=[super initWithFrame:frame];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    self.dealerArray =[NSMutableArray array];
    
    NSData *dealerdata=[user objectForKey:@"dealerdata"];
    self.dealerArray = [NSKeyedUnarchiver unarchiveObjectWithData:dealerdata];
    
    CGRect rect=frame;
    rect.size.height=44*5;
    rect.origin.x=0;
    rect.origin.y=0;
    
    _backGroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 0)];
    _backGroundView.layer.cornerRadius=2;
    _backGroundView.layer.borderColor=[UIColor grayColor].CGColor;
    _backGroundView.layer.borderWidth=2.0;
    [self addSubview:_backGroundView];
    _dealerTabView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 0) style:UITableViewStylePlain];
    _dealerTabView.delegate=self;
    _dealerTabView.dataSource=self;
    _dealerTabView.bounces=YES;
    _dealerTabView.layer.cornerRadius=2;
    _dealerTabView.layer.borderWidth=0.5;
    _dealerTabView.layer.borderColor=[UIColor lightGrayColor].CGColor;
    [self addSubview:_dealerTabView];
    [UIView animateWithDuration:0.3 animations:^{
        self.frame=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, rect.size.height);
        _backGroundView.frame=rect;
        _dealerTabView.frame=rect;
        
    } completion:^(BOOL finished) {
        if (finished) {
            
        }
    }];
    [superView addSubview:self];
    return self;
}
#pragma mark - TableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dealerArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"dealercell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    if([self.dealerArray count]>0)
    {
        DealerModle *dealer=[self.dealerArray objectAtIndex:indexPath.row];
        cell.textLabel.text=dealer.AscName;
        cell.textLabel.textAlignment=NSTextAlignmentCenter;
    }
    
    
    return cell;
}

#pragma mark - TableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect rect=_dealerTabView.frame;
    rect.size.height=0;
    DealerModle *dealer=[self.dealerArray objectAtIndex:indexPath.row];
    [UIView animateWithDuration:0.3 animations:^{
        
        _backGroundView.frame=rect;
        _dealerTabView.frame=rect;
        
    } completion:^(BOOL finished) {
        if (finished) {
            if(self.selectBlock)
            {
                self.selectBlock(dealer);
            }
            [self removeFromSuperview];
            
            
        }
    }];
    
}


@end

